﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using GalaSoft.MvvmLight.Command;
using PPP.ViewModel.BaseModel;

namespace PPP.ViewModel.WindowModel {
    /// <summary>
    /// FileViewModel for a picture gallery. 
    /// 
    /// <list type="bullet">
    /// <item>
    /// <term>Author</term>
    /// <description>Sebastian Will</description>
    /// </item>
    /// </list>
    /// 
    /// </summary>
    public class FileViewModel : PaneViewModel {
        static ImageSourceConverter ISC = new ImageSourceConverter();
        public FileViewModel(string filePath) {
            FilePath = filePath;
            Title = FileName;

            //Set the icon only for open documents (just a test)
            //IconSource = ISC.ConvertFromInvariantString(@"pack://application:,,/Images/document.png") as ImageSource;
            IconSource = ISC.ConvertFromInvariantString(@"pack://application:,,,/PPP;component/Images/document.png") as ImageSource;            
        }

        public FileViewModel() {
            IsDirty = true;
            Title = FileName;
        }

        #region FilePath
        private string _filePath = null;
        public string FilePath {
            get { return _filePath; }
            set {
                if (_filePath != value) {
                    _filePath = value;
                    RaisePropertyChanged("FilePath");
                    RaisePropertyChanged("FileName");
                    RaisePropertyChanged("Title");

                    if (File.Exists(_filePath)) {
                        _textContent = File.ReadAllText(_filePath);
                        ContentId = _filePath;
                    }
                }
            }
        }
        #endregion


        public string FileName {
            get {
                if (FilePath == null)
                    return "Noname" + (IsDirty ? "*" : "");

                return System.IO.Path.GetFileName(FilePath) + (IsDirty ? "*" : "");
            }
        }



        #region TextContent

        private string _textContent = string.Empty;
        public string TextContent {
            get { return _textContent; }
            set {
                if (_textContent != value) {
                    _textContent = value;
                    RaisePropertyChanged("TextContent");
                    IsDirty = true;
                }
            }
        }

        #endregion

        #region IsDirty

        private bool _isDirty = false;
        public bool IsDirty {
            get { return _isDirty; }
            set {
                if (_isDirty != value) {
                    _isDirty = value;
                    RaisePropertyChanged("IsDirty");
                    RaisePropertyChanged("FileName");
                }
            }
        }

        #endregion

        #region SaveCommand
        RelayCommand<object> _saveCommand = null;
        public ICommand SaveCommand {
            get {
                if (_saveCommand == null) {
                    _saveCommand = new RelayCommand<object>((p) => OnSave(p), (p) => CanSave(p));
                    
                }

                return _saveCommand;
            }
        }

        private bool CanSave(object parameter) {
            return IsDirty;
        }

        private void OnSave(object parameter) {
            //MainViewModel.This.Save(this, false);
        }

        #endregion

        #region SaveAsCommand
        RelayCommand<object> _saveAsCommand = null;
        public ICommand SaveAsCommand {
            get {
                if (_saveAsCommand == null) {
                    _saveAsCommand = new RelayCommand<object>((p) => OnSaveAs(p), (p) => CanSaveAs(p));
                }

                return _saveAsCommand;
            }
        }

        private bool CanSaveAs(object parameter) {
            return IsDirty;
        }

        private void OnSaveAs(object parameter) {
           // MainViewModel.This.Save(this, true);
        }

        #endregion

        #region CloseCommand
        RelayCommand<object> _closeCommand = null;
        public ICommand CloseCommand {
            get {
                if (_closeCommand == null) {
                    _closeCommand = new RelayCommand<object>((p) => OnClose(), (p) => CanClose());
                }

                return _closeCommand;
            }
        }

        private bool CanClose() {
            return true;
        }

        private void OnClose() {
            //MainViewModel.This.Close(this);
        }
        #endregion

    }
}
